package com.liukai.novel_type.adapter;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.liukai.novel_commons.entities.NovelType;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_type.fragment.TypePagerFragment;

import java.util.ArrayList;
import java.util.List;

public class NovelTypePagerAdapter extends FragmentPagerAdapter {

    // 小说分类集合
    private List<NovelType> mNovelTypeList = new ArrayList<>();

    public NovelTypePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        NovelType novelType = mNovelTypeList.get(position);
        LogUtils.d(this.getClass(),"getPageTitle:" + novelType.toString());
        return mNovelTypeList.get(position).getTypeName();
    }

    @Override
    public Fragment getItem(int position) {
        TypePagerFragment typePagerFragment = new TypePagerFragment(mNovelTypeList.get(position));
        return typePagerFragment;
    }

    @Override
    public int getCount() {
//        LogUtils.d(this.getClass(), "getCount：" + this.mNovelTypeList.size());
        return mNovelTypeList.size();
    }

    /**
     * 设置小说分类
     * @param novelTypeList
     */
    public void setNovelType(List<NovelType> novelTypeList) {
        if (this.mNovelTypeList != null)
            this.mNovelTypeList.clear();
        this.mNovelTypeList.addAll(novelTypeList);
        notifyDataSetChanged();
    }

}
